import { _decorator, Component, Node, Prefab, instantiate, Vec3, UITransform } from 'cc';
import { gameFruit } from './gameFruit';
import gameFruitUtils from './gameFruitUtils';
const { ccclass, property } = _decorator;

@ccclass('Fruit')
class Fruit {

    @property({ type: String, tooltip: "名称" })
    name: string = '';

    @property({ type: Number })
    initPoolCount: number = 0;

    @property({ type: Number, tooltip: "切中时可获得的积分" })
    score: number = 0;

    @property({ type: String, tooltip: "类型（水果，炸弹）" })
    type: string = '';

    @property({ type: Prefab })
    prefab: Prefab = null;

}
/**
 * 主界面-水果组（负责随机生成水果）
 */
@ccclass('gameFruitMainFruitGroup')
export class gameFruitMainFruitGroup extends Component {

    @property({ type: Number })
    maxLength: number = 4;

    @property({ serializable: true, type: [Fruit] })
    fruitG: Fruit[] = [];

    start() {

    }

    update(deltaTime: number) {
        // console.log('maxLength :{}',this.maxLength)
        // console.log(this.fruitG)
    }

    createFruitList() {
        //console.log('createFruitList');
        //console.log("fruitG :{}", this.fruitG);

        let apple = this.fruitG[0];
        //console.log("apple :{}", apple);

        let prefab = apple.prefab;

        let fruitNode = instantiate(prefab);
        this.node.addChild(fruitNode);

        const curNodeUiTransform = this.node.getComponent(UITransform);
        const fruitNodeUiTransform = fruitNode.getComponent(UITransform);
        let x = gameFruitUtils.generateRandomNumber(-curNodeUiTransform.width / 2 + fruitNodeUiTransform.width / 2, curNodeUiTransform.width / 2 - fruitNodeUiTransform.width / 2);
        let y = -(curNodeUiTransform.height / 2 - fruitNodeUiTransform.height / 2)
        console.log("createFruitList {}-{}", x, y)
        fruitNode.setPosition(new Vec3(x, y, 0));

        //通过节点获取脚本组件
        let gameFruitScript = fruitNode.getComponent(gameFruit);
        gameFruitScript.init()
    }
}


